package com.liam.game.ui.plane;

import com.liam.game.util.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Vector;

public class GamePanel extends JPanel {

    BufferedImage bg;

    Hero hero = new Hero();

    // 敌人
    List<Ep> epList = new Vector<>();

    // 子弹
    List<Fire> fires = new Vector<>();

    int score;

    // 火力
    int power = 1;

    // 游戏接收时为true
    boolean gameOver = false;

    public GamePanel(GameFrame frame) {
        setBackground(Color.pink);
        bg = Utils.getImg("static/img/bg.png");

        // 鼠标监听
        MouseAdapter adapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(gameOver){

                    epList.clear();
                    fires.clear();
                    //重新开始
                    hero = new Hero();
                    score = 0;
                    gameOver = false;
                    power = 1;
                    repaint();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                // 鼠标在游戏界面移动
                if(!gameOver){
                    hero.moveToMouse(e.getX(), e.getY());
                    repaint();
                }

            }
        };

        addMouseListener(adapter);
        addMouseMotionListener(adapter);

        // 键盘监听
        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    hero.moveUp();
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    hero.moveDown();
                } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    hero.moveLeft();
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    hero.moveRight();
                }
                repaint();
            }
        };
        // 加入到窗体的键盘监听器
        frame.addKeyListener(keyAdapter);

    }


    //
    public void action() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    if(!gameOver){
                        // 敌人入场
                        epEnter();
                        // 敌人移动
                        epMove();

                        // 射击子弹
                        heroShoot();
                        // 子弹移动
                        fireMove();

                        // 射中
                        shootEp();

                        // 飞机相撞
                        hit();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    repaint();

                }
            }

        }.start();

    }

    private void hit() {
        for (int i = 0; i < epList.size(); i++) {
            Ep e = epList.get(i);
            //
            if(e.hit(hero)){
                // 删除敌人
                epList.remove(e);
                hero.hp -- ;
                score ++;

                if(hero.hp <= 0){
                    gameOver = true;
                }

            }
        }
    }

    /**
     * 判断是否击中
     */
    private void shootEp() {
        for (int i = 0; i < fires.size(); i++) {
            Fire f = fires.get(i);
            bang(f);
        }

    }

    /**
     * 击中敌人
     * @param f
     */
    private void bang(Fire f) {
        for (Ep ep : epList) {
            if (ep.shootByFire(f)) {
                ep.hp --;
                if(ep.hp <= 0){
                    // ep 消失
                    epList.remove(ep);
                    score += 1;
                    if(ep.type == 12){
                        power ++;
                        if(power > 3){
                            hero.hp++;
                            power = 3;
                        }
                    }

                }

                fires.remove(f);

                break;
            }
        }
    }

    int shootIndex = 0;

    private void heroShoot() {
        shootIndex++;
        if (shootIndex >= 10) {
            if(power == 1){

                Fire fire3 = new Fire(hero.x + 75, hero.y - 30);
                fires.add(fire3);
            }else if(power == 2){
                Fire fire = new Fire(hero.x, hero.y);
                fires.add(fire);

                // 双排子弹
                Fire fire2 = new Fire(hero.x + 150, hero.y);
                fires.add(fire2);

            }else{
                Fire fire = new Fire(hero.x, hero.y);
                fires.add(fire);

                Fire fire3 = new Fire(hero.x + 75, hero.y - 30);
                fires.add(fire3);

                // 双排子弹
                Fire fire2 = new Fire(hero.x + 150, hero.y);
                fires.add(fire2);

            }
            shootIndex = 0;
        }

    }

    private void fireMove() {
        for (Fire f : fires) {
            f.move();
        }
    }

    private void epMove() {
        for (Ep ep : epList) {
            ep.move();
        }
    }

    /**
     * 敌人入场
     */
    int index = 0;

    private void epEnter() {
        index++;
        if (index >= 20) {
            Ep e = new Ep();
            epList.add(e);
            index = 0;
        }
    }


    /**
     * 专用画图方法
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(bg, 0, 0, null);

        // 画图有顺序，先画会被后画覆盖
        g.drawImage(hero.img, hero.x, hero.y, null);

        for (int i = 0; i < epList.size(); i++) {
            Ep ep = epList.get(i);
            g.drawImage(ep.img, ep.x, ep.y, null);
        }

        // todo 强for 有线程安全问题
        for (int i = 0; i < fires.size(); i++) {
            Fire f = fires.get(i);
            g.drawImage(f.img, f.x, f.y, f.w, f.h, null);
        }

        g.setColor(Color.white);
        g.setFont(new Font("楷体",Font.BOLD,20));
        g.drawString("分数："+score,10,30);

        // 几条命
        for(int i=0;i<hero.hp;i++){
            g.drawImage(hero.img, 350+i*35, 5, 30, 30, null);
        }

        if(gameOver){
            g.setColor(Color.RED);
            g.setFont(new Font("楷体",Font.BOLD,35));
            g.drawString("GAME OVER",180,300);

            g.setColor(Color.GREEN);
            g.setFont(new Font("楷体",Font.BOLD,20));
            g.drawString("点击屏幕重新开始",180,350);
        }
    }
}
